﻿using HCQ2_Common.SQL;
using HCQ2_IDAL;
using HCQ2_Model.VRQDMeta.InputDto._20220112;
using HCQ2_Model.VRQDMeta.OutputDto._20220112;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HCQ2_DAL_MSSQL
{
    public partial class T_DocumentInfoLogDAL: IT_DocumentInfoLogDAL
    {
        /// <summary>
        ///  获取待审核应用列表
        /// </summary>
        /// <param name="param"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<AppPendingListOutput> GetMetaAppPendingList(AppPendingListInput param, int user_id, out int total)
        {
            sb?.Clear();
            sb.Append($@"(SELECT file_id AS fileID,file_name AS fileName,file_note AS fileNote,file_code AS fileCode,
            file_order AS fileOrder,file_status AS fileStatus,doc_number AS verNumber,folder_id AS folderID,
            folderName=(SELECT folder_name FROM dbo.T_DocumentFolder WHERE folder_id=l.folder_id),
            attach_url AS attachUrl,image_url AS imageUrl,file_money AS filePrice,file_size AS fileSize,
            pag_num AS playerNum,finish_time,CONVERT(varchar(100),update_time,120)AS updateTime,
            update_name AS updateName,CONVERT(varchar(100),create_time,120)AS createTime,
            create_name AS createName,createImage=(SELECT user_img FROM dbo.T_User WHERE user_id=l.create_id),
            listPrice AS prices,ROW_NUMBER() OVER(ORDER BY create_time DESC)AS num
            FROM T_DocumentInfoLog l WHERE file_status=1 ");
            if (!string.IsNullOrEmpty(param.folderPath))
                sb.Append($@" AND EXISTS(SELECT folder_id FROM dbo.T_DocumentFolderRelation WHERE folder_path LIKE '{param.folderPath}%' AND folder_id=l.folder_id) ");
            if (!string.IsNullOrEmpty(param.keyword))
                sb.Append($" AND (file_name LIKE '%{param.keyword}%' OR file_note LIKE '%{param.keyword}%') ");
            if (user_id > 0)
                sb.Append($" AND create_id={user_id} ");
            sb.Append(")logs ");
            total= SqlHelper.ExecuteScalarInt32($"SELECT COUNT(0) FROM  {sb}");
            return SqlHelper.ExecuteList<AppPendingListOutput>($"SELECT TOP {param.rows} * FROM  {sb} WHERE logs.num>{(param.page - 1) * param.rows}");
        }
        /// <summary>
        ///  获取应用商品详情
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public MetaAppDetailOutput GetMetaAppDetail(MetaAppDetailInput param,int user_id)
        {
            sb?.Clear();
            sb.Append($@"SELECT * FROM 
            (SELECT file_id AS fileID,file_name AS fileName,file_note AS fileNote,file_status AS fileStatus,
            file_code AS fileCode,file_order AS fileOrder,doc_number AS verNumber,
            file_money AS filePrice,attach_url AS attachUrl,pag_num AS playerNum,finish_time,
            isStar=(SELECT CAST(COUNT(0) AS BIT) FROM T_DocumentUserStar WHERE file_id={param.fileID} AND create_id={user_id}),
            isHold=(SELECT CAST(COUNT(0) AS BIT) FROM T_VRFavoriteGoods WHERE file_id={param.fileID} AND user_id={user_id}),
            isPaid=(SELECT CAST(COUNT(0) AS BIT) FROM dbo.N_OrderCommodityInfo WHERE file_id={param.fileID} AND OrderCode IN(
	            SELECT OrderCode FROM dbo.N_Order WHERE OrderCreateUserId={user_id} AND OrderState IN(2,4))),
            image_url AS imageUrl,file_size AS fileSize,CONVERT(varchar(100),issue_start,120) AS releaseTime,
            vr_star AS starNum,browse_num AS browseNum,hold_num AS holdNum,down_number AS downNum,
            pay_num AS payNum,CONVERT(varchar(100),update_time,120) AS updateTime,update_name AS updateName,
            CONVERT(varchar(100),create_time,120) AS createTime,create_name AS createName,create_id AS createID,
            createImage=(SELECT user_img FROM dbo.T_User WHERE user_id=i.create_id) FROM dbo.T_DocumentInfo i
            WHERE file_id={param.fileID}) f INNER JOIN
            (SELECT file_id,folder_id AS folderID FROM dbo.T_DocumentFolderRelation n)r ON f.fileID=r.file_id INNER JOIN 
            (SELECT folder_id,folder_name AS folderName,bs_type AS bsType FROM dbo.T_DocumentFolder)d ON r.folderID=d.folder_id LEFT JOIN 
            (SELECT * FROM (SELECT ROW_NUMBER() OVER(PARTITION BY file_id ORDER BY create_time DESC)AS num,
            file_id,file_status AS versionStatus FROM dbo.T_DocumentInfoLog)g WHERE g.num=1)logs ON f.fileID=logs.file_id;");
            return SqlHelper.ExecuteList<MetaAppDetailOutput>(sb.ToString())?.FirstOrDefault();
        }
        /// <summary>
        ///  获取应用版本详情
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public MetaAppVersionOutput GetMetaAppVersionDetail(MetaAppVersionInput param)
        {
            sb?.Clear();
            sb.Append($@"SELECT TOP 1 file_id AS fileID,file_name AS fileName,file_note AS fileNote,
            pag_num AS playerNum,folder_id AS folderID,folder_name AS folderName,
            attach_url AS attachUrl,image_url AS imageUrl,doc_number AS verNumber,file_size 
            AS appSize,listPrice AS prices,CONVERT(varchar(100),create_time,120) AS createTime, 
            createImage=(SELECT TOP 1 user_img FROM dbo.T_User WHERE user_id=g.create_id),
            g.create_name AS createName,g.create_id AS createID 
            FROM dbo.T_DocumentInfoLog g WHERE file_id={param.fileID} ORDER BY create_time DESC;");
            return SqlHelper.ExecuteList<MetaAppVersionOutput>(sb.ToString())?.FirstOrDefault();
        }
        /// <summary>
        ///  获取应用版本历史列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<MetaAppVersionListOutput> GetMetaAppVersionList(MetaAppVersionListInput param)
        {
            sb?.Clear();
            sb.Append($@"SELECT file_id AS fileID,file_name AS fileName,file_note AS fileNote,
            pag_num AS playerNum,folder_id AS folderID,folder_name AS folderName,
            attach_url AS attachUrl,image_url AS imageUrl,doc_number AS verNumber,file_size 
            AS appSize,listPrice AS prices,CONVERT(varchar(100),create_time,120) AS createTime, 
            CONVERT(varchar(100),approve_time,120) AS approvalTime,g.approve_note AS approvalNote
            FROM dbo.T_DocumentInfoLog g WHERE file_id={param.fileID} ");
            if (param.fileStatus > -1)
                sb.Append($" AND g.file_status={param.fileStatus} ");
            sb.Append(" ORDER BY update_time DESC;");
            return SqlHelper.ExecuteList<MetaAppVersionListOutput>(sb.ToString());
        }
    }
}